<template>
	<view>
		<view class="head-body">
			<uni-section title="工单信息" type="line" titleFontSize="20px">
				<uni-row class="demo-uni-row" :width="nvueWidth">
					<uni-col :span="4">
						<view class="gd-text">SN号:</view>
					</uni-col>
					<uni-col :span="14">
						<uni-easyinput trim="all" v-model="mo" placeholder=""></uni-easyinput>
					</uni-col>
					<uni-col :span="6">
						<yt-scanCode btnName="扫码" style="margin-left: 18%;margin-top: 3px;"
							@getScanCode="OnScanningCode"></yt-scanCode>
					</uni-col>
				</uni-row>
				<uni-row class="demo-uni-row" :width="nvueWidth">
					<uni-col :span="4">
						<view class="gd-text">工序:</view>
					</uni-col>
					<uni-col :span="14">
						<uni-easyinput disabled v-model="jobName"></uni-easyinput>
					</uni-col>
				</uni-row>
			</uni-section>
			<view style="border-top: 1px solid #cecece; margin-top: 30px;">
			</view>
			<uni-section title="物料信息" type="line" titleFontSize="20px">
				<uni-card>
					<view style="height:55vh">
						<zb-table :columns="column" :stripe="true" :border="true" :data="data"></zb-table>
					</view>
				</uni-card>
			</uni-section>
		</view>
		<uni-popup ref="Dialog" type="dialog">
			<uni-popup-dialog ref="inputClose" mode="input" title="核对散料信息" :before-close="true"
				@confirm="dialogInputConfirm" @close="dialogInputConfirmclose">
				<uni-forms ref="customForm" :rules="customRules" labelWidth="90px" :modelValue="customFormData">
					<uni-forms-item label="物料号:" name="mateCode">
						<uni-easyinput v-model="customFormData.mateCode" placeholder="" :disabled="true" />
					</uni-forms-item>
					<uni-forms-item label="需求数量：" name="reciveNum">
						<uni-easyinput v-model="customFormData.reciveNum" placeholder="" :disabled="true" />
					</uni-forms-item>
					<uni-forms-item label="已核数量" name="installNum">
						<uni-easyinput v-model="customFormData.installNum" placeholder="" :disabled="true" />
					</uni-forms-item>
					<uni-forms-item label="未核数量" name="inum">
						<uni-easyinput v-model="customFormData.inum" placeholder="" />
					</uni-forms-item>
				</uni-forms>
			</uni-popup-dialog>
		</uni-popup>
		<sl-pda-employ :config="pdaConfig"></sl-pda-employ>
	</view>
</template>

<script>
	String.prototype.myReplace = function(f, e) { //吧f替换成e
		var reg = new RegExp(f, "g"); //创建正则RegExp对象   
		return this.replace(reg, e);
	}
	import {
		btnSelectQgLeftListByMo,
		getAllBulkMates,
		saveBulkMate
	} from '@/api/system/BulkMaterial'
	import slPdaEmploy, {
		initPdaScan,
		manualTrigScan,
		onUnloadPdaScan,
		BroadcastConfig
	} from '../../uni_modules/sl-pdaEmploy'
	export default {
		data() {
			return {
				pdaConfig: { // 可以抽离出去管理这个配置
					BroadcastAction: 'nantong',
					BroadcastLabelKey: 'data',
					//BroadcastTriglKey: 'com.scan.onStartScan',
				},
				nvueWidth: uni.getWindowInfo().screenWidth,
				mo: "",
				jobName: "",
				data: [],
				customRules: {
					PartNumber: {
						rules: [{
							required: true,
							errorMessage: '料号不能为空'
						}]
					},
					reciveNum: {
						rules: [{
							required: true,
							errorMessage: '核对数量不能为空'
						}, {
							format: 'number',
							errorMessage: '核对数量只能输入数字'
						}]
					}
				},
				customFormData: {

				},
				column: [{
						label: '序号',
						type: 'index',
						align: "center",
						width: uni.getWindowInfo().screenWidth / 10 * 1.8
					},
					{
						name: 'mateName',
						label: '料号',
						align: "center",
						width: uni.getWindowInfo().screenWidth / 10 * 4
					},
					{
						name: 'reciveNum',
						align: "center",
						label: '需求数量',
						width: uni.getWindowInfo().screenWidth / 10 * 2
					},
					{
						name: 'installNum',
						label: '核对数量',
						align: "center",
						width: uni.getWindowInfo().screenWidth / 10 * 2
					}
				]
			}
		},
		components: {
			slPdaEmploy
		},
		onLoad() {
			initPdaScan(code => {
				console.log(code);
				this.clear();
				let packagingCode = code.split(" ");
				if (packagingCode.length === 3) {
					this.MateLoading(packagingCode[1], packagingCode[2]);

				} else {
					this.ListBySn(code);
					this.mo = code;
				}
			})
		},
		onUnload() {
			onUnloadPdaScan()
		},
		methods: {
			MateLoading(num, mateCode) {
				let p = 0;
				for (var i = 0; i < this.data.length; i++) {
					console.log(this.data[i].mateCode, mateCode)
					if (this.data[i].mateCode === mateCode) {
						p = 1;
						if (this.data[i].reciveNum - this.data[i].installNum <= 0) {
							this.$modal.loading(this.data[i].mateName + "，超出数量");
							setTimeout(function() {
								uni.hideLoading();
							}, 3000);
						} else {
							this.customFormData = this.data[i];
							this.customFormData.inum = this.data[i].reciveNum - this.data[i].installNum;
							this.$refs.Dialog.open();
						}
					}
				}
				if (p === 0) {
					uni.showToast({
						title: "无匹配物料",
						icon: "error",
						duration: 2000
					})
				}
			},
			ListBySn(sn) {
				btnSelectQgLeftListByMo(sn).then(res => {
					// console.log(res.data, res.code);
					if (res.code === 200) {
						this.jobName = res.data.jobName;
						getAllBulkMates(res.data.jobId, res.data.jobCode, "").then(res => {
							console.log(res.data);
							this.data = res.data;
						})
					}
				})
			},
			dialogInputConfirm() {
				let data = this.customFormData;
				if (isNaN(data.inum) || data.inum === 0) {
					uni.showToast({
						title: "数量为空",
						icon: "error",
						duration: 2000
					})
					return
				} else if ((data.reciveNum - data.installNum - data.inum) < 0 ) {
					uni.showToast({
						title: "数量超出",
						icon: "error",
						duration: 2000
					})
					return
				}
				let newData = {
					jobId: data.jobId,
					mateId: data.mateId,
					mateCode: data.mateCode,
					mateName: data.mateName,
					mateDesignnumber: data.mateDesignnumber,
					mateType: data.mateType,
					installNum: data.inum,
					partCode: data.mateCode,
					installPlace: ''
				}
				const userId = this.$store.state.user.userAll.userId;
				const userName = this.$store.state.user.userAll.userName;
				console.log(newData, userId, userName)
				saveBulkMate(newData, userId, userName).then(res => {
					this.$refs.Dialog.close();
					this.ListBySn(this.mo);
				})
			},
			OnScanningCode(code) {
				console.log(code);
				this.clear();
				let packagingCode = code.split(" ");
				if (packagingCode.length === 3) {
					this.MateLoading(packagingCode[1], packagingCode[2]);
				} else {
					this.ListBySn(code);
					this.mo = code;
				}
			},
			dialogInputConfirmclose() {
				this.$refs.Dialog.close();
			},
			clear() {
				this.customFormData = {};
			}
		}
	}
</script>

<style lang="scss">
	.head-top {
		height: 10vh;
	}

	.head-top-row {
		background-color: #007AFF;
		display: flex;
		height: 6vh;
	}

	.head-top-row-left {
		padding-top: 0.5vh;
		font-size: 19px;
		font-weight: 500;
		width: 86%;
	}

	.head-top-row-left-text {
		padding-left: 30%;
		color: #ffffff;
	}

	.head-top-row-right {
		color: #ffffff;
		margin-left: 2%;
		width: 14%;
		font-size: 17px;
		font-weight: 500;
		padding-top: 1vh;
	}

	.head-body {
		background-color: #ffffff;
		height: 90vh;
	}

	.demo-uni-row {
		padding-left: 10px;
		margin-top: 6px;
	}

	.gd-text {
		font-size: 17px;
		margin-top: 4px;
		float: right;
		margin-right: 6px;
	}

	::v-deep {
		.uni-card {
			margin: 1% !important;
			padding: 0 !important;

			.uni-card__content {
				padding: 0 !important;
			}
		}
	}

	.getScanCode {
		margin-right: 4px;
		margin-top: 10px;
	}

	::v-deep .is-disabled {
		color: black !important;
	}

	.triangle-facing-left {
		display: inline-block;
		border-left: 1px solid;
		border-bottom: 1px solid;
		width: 12px;
		height: 12px;
		transform: rotate(45deg);
	}
</style>